//
//  RippleHeaderView.m
//  cloudCall
//
//  Created by zhang on 2021/11/1.
//

#import "RippleHeaderView.h"



@interface RippleHeaderView ()

/**imageView*/
@property (nonatomic, strong)UIImageView *headerView;

@end


@implementation RippleHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.headerView = [[UIImageView alloc] initWithFrame:CGRectMake(6, 6, frame.size.width-12, frame.size.height-12)];
        self.headerView.backgroundColor = [UIColor orangeColor];
        self.headerView.layer.cornerRadius = (frame.size.height/2-6);
        self.headerView.image = [UIImage imageNamed:@"ImageSource.bundle/user_default@3x"];
        self.headerView.layer.masksToBounds = YES;
        self.layer.cornerRadius = frame.size.height/2;
    }
    return self;
}


-(void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [self startAnimation];
}

-(void)startAnimation{
    UIColor *fromColor = [UIColor colorWithHexString:@"#BFBFBF"];
    UIColor *toColor = [UIColor colorWithHexString:@"#BFBFBF"];
    if ([self.model.gender isEqualToString:@"M"]) {
        fromColor = [UIColor colorWithHexString:@"#5FCAED"];
        toColor = [UIColor colorWithHexString:@"#3D8AE4"];
    } else if ([self.model.gender isEqualToString:@"F"]) {
        fromColor = [UIColor colorWithHexString:@"#FF8787"];
        toColor = [UIColor colorWithHexString:@"#FF55B8"];
    }
    NSInteger pulsingCount = 3;
    double animationDuration = 3;
    UIImage *holder = [UIImage imageNamed:@"ImageSource.bundle/user_default@3x"];
    if ([self.model.gender isEqualToString:@"M"]) {
        holder = [UIImage imageNamed:@"ImageSource.bundle/user_man@3x"];
    } else if ([self.model.gender isEqualToString:@"F"]) {
        holder = [UIImage imageNamed:@"ImageSource.bundle/user_woman@3x"];
    }
    [self.headerView sd_setImageWithURL:[NSURL URLWithString:self.model.avatarUrl] placeholderImage:holder];
    CGRect rect = self.bounds;
    CAGradientLayer * animationLayer = [self setGradualChangingColorRect:rect fromColor:fromColor toColor:toColor];
    for (int i = 0; i < pulsingCount; i++) {
        CALayer * pulsingLayer = [self setGradualChangingColorRect:rect fromColor:fromColor toColor:toColor];
        pulsingLayer.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
        pulsingLayer.borderColor = [UIColor colorWithHexString:@"#191919"].CGColor;
        pulsingLayer.borderWidth = 3;
        CAMediaTimingFunction * defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
        CAAnimationGroup * animationGroup = [CAAnimationGroup animation];
        animationGroup.fillMode = kCAFillModeBackwards;
        animationGroup.beginTime = CACurrentMediaTime() + (double)i * animationDuration / (double)pulsingCount;
        animationGroup.duration = animationDuration;
        animationGroup.repeatCount = HUGE;
        animationGroup.timingFunction = defaultCurve;
        CABasicAnimation * scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnimation.fromValue = @1;
        scaleAnimation.toValue = @1.4;
        CAKeyframeAnimation * opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
        opacityAnimation.values = @[@1, @0.9, @0.8, @0.7, @0.6, @0.5, @0.4, @0.3, @0.2, @0.1, @0];
        opacityAnimation.keyTimes = @[@0, @0.1, @0.2, @0.3, @0.4, @0.5, @0.6, @0.7, @0.8, @0.9, @1];
        animationGroup.animations = @[scaleAnimation, opacityAnimation];
        [pulsingLayer addAnimation:animationGroup forKey:@"plulsing"];
        [animationLayer addSublayer:pulsingLayer];
    }
    [self.layer addSublayer:animationLayer];
    [self addSubview:self.headerView];
}

- (CAGradientLayer *)setGradualChangingColorRect:(CGRect)rect fromColor:(UIColor *)fromColor toColor:(UIColor *)toColor{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = rect;
    gradientLayer.colors = @[(__bridge id)fromColor.CGColor,(__bridge id)toColor.CGColor];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    gradientLayer.cornerRadius = (rect.size.height/2);
    gradientLayer.locations = @[@0,@1];
    return gradientLayer;
}




@end
